package com.lxw.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EnumConstraintCaliator implements ConstraintValidator<EnumValidate,Integer> {

    private Set<Integer> set = new HashSet();
//      private String regexp = null;

    @Override
    public void initialize(EnumValidate constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        int[] values = constraintAnnotation.values();
        for (int value : values) {
            set.add(value);
            System.out.println("注解传入value = " + value);
        }
//        获取正则
//         regexp = constraintAnnotation.regexp();



    }

    @Override
    public boolean isValid(Integer v, ConstraintValidatorContext constraintValidatorContext) {
        System.out.println("表单传入value = "+ v);
////使用正则验证
//        Pattern compile = Pattern.compile(regexp);
//        Matcher matcher = compile.matcher(v.toString());
//        boolean matches = matcher.matches();
//        System.out.println("match = " + matcher);
//        System.out.println("matches = " + matches);
//        return matcher.matches();
        return set.contains(v);
    }
}
